<template>
  <div class="page-container">
    <div class="tool-container">
      <el-form class="tool-search" :model="queryParams" ref="queryFormRef" :inline="true">
        <el-form-item prop="regionCode">
          <RegionSelect v-model="queryParams.regionCode" placeholder="行政区域" clearable />
        </el-form-item>
        <el-form-item prop="name">
          <el-input v-model="queryParams.name" placeholder="房站名称" clearable @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item prop="status">
          <DictSelect dict="station_status" v-model="queryParams.status" placeholder="状态" clearable />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery" v-hasPermi="['biz:station:list']">搜索</el-button>
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
      <div class="tool-handle">
        <el-button type="primary" icon="Plus" @click="handleAdd" v-hasPermi="['biz:station:add']">新增房站</el-button>
      </div>
    </div>
    <div class="tabel-container">
      <vxe-grid ref="tableRef" v-bind="gridOptions">
        <template #status="{ row }">
          <div v-if="row.status==1">
            <el-tooltip
        class="box-item"
        effect="dark"
        :content="checkError(row)"
        placement="right"
      >
          <dict-tag :options="station_status" :value="row.status" />
      </el-tooltip>
        </div>
        <div v-else>
          <dict-tag :options="station_status" :value="row.status" />
        </div>
        </template>
        <template #mode="{ row }">
          <el-switch v-model="row.operationalMode" active-value="1" inactive-value="2" :before-change="() => handelModeChange(row)" />
        </template>
        <template #handle="{ row }">
          <el-button link type="primary" @click="handleDetail(row)" v-hasPermi="['biz:station:query']">详情</el-button>
          <el-button link type="primary" @click="handleUpdate(row)" v-hasPermi="['biz:station:edit']">编辑</el-button>
          <el-button link type="danger" @click="handleDelete(row)" v-hasPermi="['biz:station:remove']">删除</el-button>
        </template>
        <template #bottom>
          <pagination
            v-show="total > 0"
            :total="total"
            v-model:page="queryParams.pageNum"
            v-model:limit="queryParams.pageSize"
            @pagination="getList"
          />
        </template>
      </vxe-grid>
    </div>
  </div>
</template>

<script setup lang="ts">
import { listStation, delStation, changeOperationalMode } from '@/api/station/index';
import { StationVO } from '@/api/station/types';
import { getTableConfig } from './columns';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { station_status,station_error_reason } = toRefs<any>(proxy?.useDict('station_status','station_error_reason'));
const gridOptions = reactive(getTableConfig());
const data = toRef(gridOptions, 'data');
const loading = toRef(gridOptions, 'loading');

const total = ref(0);

const queryFormRef = ref<ElFormInstance>();

const queryParams = ref<PageQuery>({
  pageNum: 1,
  pageSize: 20,
  regionCode: '',
  name: '',
  status: ''
});

const checkError=(row)=>{
  if(station_error_reason.value==null){
    return "";
  }else{
    let error="";
    station_error_reason.value.forEach(s => {
      if(row[s.value]==1){
        error+=s.label;
        error+=" ";
      }
    });
    return error;
  }
};

/** 查询租户列表 */
const getList = async () => {
  loading.value = true;
  const res = await listStation(queryParams.value);
  let datas=res.rows;
  datas.forEach(d=>{
    d.concentration/=10.0;
  });
  data.value = datas;
  total.value = res.total;
  loading.value = false;
};

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

/** 搜索重置操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};
/** 新增按钮操作 */
const handleAdd = () => {
  proxy?.$tab.openPage('/station/stationMessage/add');
};

/** 修改按钮操作 */
const handleUpdate = async (row?: StationVO) => {
  proxy?.$tab.openPage('/station/stationMessage/edit', '', { id: row?.stationId });
};
/** 详情按钮操作 */
const handleDetail = async (row?: StationVO) => {
  proxy?.$tab.openPage('/station/stationMessage/info', '', { id: row?.stationId });
};

/** 删除按钮操作 */
const handleDelete = async (row?: StationVO) => {
  const _ids = row?.stationId || '';
  await proxy?.$modal.confirm('是否确认删除站房"' + row?.name + '"的数据项？');
  loading.value = true;
  await delStation(_ids).finally(() => (loading.value = false));
  await getList();
  proxy?.$modal.msgSuccess('删除成功');
};
const handelModeChange = async (row: StationVO) => {
  let mode = row.operationalMode == '2' ? '1' : '2';
  const res = await changeOperationalMode(row.stationId, mode);
  if (res.code == 200) {
    proxy?.$modal.msgSuccess('操作成功！');
    return true;
  } else {
    return false;
  }
};
onMounted(() => {
  getList();
});
</script>
